package functionTypes;

import Abstracts.AbstractSinglePropertyFunction;

public class OldSinglePropertyFunction implements AbstractSinglePropertyFunction, Cloneable{

	public int type;
	public double[] coeff;
	public int range = 100;

	public OldSinglePropertyFunction clone() throws CloneNotSupportedException{
		OldSinglePropertyFunction obj=(OldSinglePropertyFunction)super.clone();
		obj.coeff = coeff.clone();
		obj.type = type;
		obj.range = range;
        return obj;
	}
	/*
	public void clone(AbstractSinglePropertyFunction f){
		f = new OldSinglePropertyFunction();
		((OldSinglePropertyFunction)f).type = this.type;
		((OldSinglePropertyFunction)f).range = this.range;
		((OldSinglePropertyFunction)f).coeff = this.coeff.clone();
	}
	*/
	public OldSinglePropertyFunction(){
		rebuild();
	}
	
	public double compute(double arg) {
		if (type == 0){
			return coeff[0]*Math.sin(coeff[1]*arg);
		}else if (type == 1){
			return coeff[0]*Math.cos(coeff[1]*arg);
		}else if (type == 2){
			return coeff[0]*Math.atan(coeff[1]*arg);
		}else if (type == 3){
			return coeff[0]*Math.pow(arg,3);
		}else if (type == 4){
			return coeff[0]*arg;
		}else{
			return coeff[0];
		}
	}

	private double getC(){
		return (Math.random()-0.5)*range;
	}
	
	public void rebuild() {
		type = (int)Math.round((Math.random()*5));
		
		if (type == 0){
			coeff = new double[]{getC(),getC()};
		}else if (type == 1){
			coeff = new double[]{getC(),getC()};
		}else if (type == 2){
			coeff = new double[]{getC(),getC()};
		}else if (type == 3){
			coeff = new double[]{getC()};
		}else if (type == 4){
			coeff = new double[]{getC()};
		}else{
			coeff = new double[]{getC()};
		}
	}

	public int getType() {
		return type;
	}
	
	public double[] getCoeff(){
		return coeff;
	}

	public void setDefaultType() {
		type = (int)Math.round((Math.random()*3));;
		if (type==3){
			type = 5;
			coeff = new double[]{Math.random()};
		}else{
			coeff = new double[]{Math.random(),getC()};
		}
	}
	
	public void setConstantType(){
		type = 5;
		coeff = new double[]{Math.random()};
	}

	public double getCoeffRange() {
		return range/2;
	}

}
